# -*- coding=utf-8 -*-
# 机油 spider
#
import  scrapy
import  json
from field.jiyouitems import JiYouItem
class JiYouSpider(scrapy.Spider):
    name = "jiyou"

    def start_requests(self):

       url="http://jy.ycqpmall.com/mall/search.php?list=0&catid=%s&x=47&y=26" #23458

       with open('data/jiyou/jiyou_cars3.json') as file:
           years=json.load(file);
           #print len(years)

           for jsonitem in  years:
               if (jsonitem['cartypeyearid']!='0'):
                   url= "http://jy.ycqpmall.com/mall/search.php?list=0&catid=%s&x=47&y=26" % jsonitem['cartypeyearid']
                   #print "url="+url
                   request= scrapy.Request(url,callback=self.parse)
                   request.meta['cartypeyearid']=jsonitem['cartypeyearid']

                   yield  request

    def start_requests2(self):

        url = "http://jy.ycqpmall.com/mall/search.php?list=0&catid=%s&x=47&y=26"  # 23458

        url = "http://jy.ycqpmall.com/mall/search.php?list=0&catid=%s&x=47&y=26" % 23458
                    # print "url="+url
        request = scrapy.Request(url, callback=self.parse)
        request.meta['cartypeyearid'] =23458

        yield request

    def parse(self, response):
        se= response.selector
        #print  se.xpath('//div[@class="list"]').extract()
        list= se.xpath('//div[@class="list"]')
        if(len(list)>0):
                jy=JiYouItem();
                p1=se.xpath('//div[@class="list"]//table//ul/li/strong/text()')[0].extract()
                jy['prop1']=''
                if(p1 is not None):
                    jy['prop1'] = p1;


                pp= se.xpath('//div[@class="list"]//table//ul/li/span/text()').extract();


                jy['prop2']=pp

                # p2 = se.xpath('//div[@class="list"]//table//ul/li/span/text()')[0].extract()
                # jy['prop2'] = ''
                # if (p2 is not None):
                #     jy['prop2'] = p2;
                #
                # p3 = se.xpath('//div[@class="list"]//table//ul/li/span/text()')[1].extract()
                # jy['prop3'] = ''
                # if (p3 is not None):
                #     jy['prop3'] = p3;
                #
                # p4 = se.xpath('//div[@class="list"]//table//ul/li/span/text()')[2].extract()
                # print "p4"+p4 +"1111";
                # jy['prop4'] = ''
                # if (p4 is not None):
                #     jy['prop4'] = p4;

                jy['cartypeyearid']=response.meta['cartypeyearid']

                print jy
                yield jy;


